/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.edu.agh.student.logic.webapp;

import java.io.Serializable;
import java.util.List;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Transactional;
import org.jboss.seam.log.Log;
import org.jbpm.api.IdentityService;
import pl.edu.agh.student.logic.service.CustomerServiceImpl;
import pl.edu.agh.student.logic.service.RoleServiceImpl;
import pl.edu.agh.student.logic.service.UserServiceImpl;
import pl.edu.agh.student.logic.util.ServiceProvider;
import pl.edu.agh.student.model.Gender;
import pl.edu.agh.student.model.Customer;
import pl.edu.agh.student.model.Role;
import pl.edu.agh.student.model.User;

/**
 *
 * @author janburkot
 */
@Stateful
@Name("personBean")
@Scope(ScopeType.CONVERSATION)
@Transactional
public class PersonBeanImpl implements PersonBean, Serializable {

    @In(value = "customerService", create = true, required = false)
    @Out
    protected CustomerServiceImpl customerService;
    @In(value="userService",create=true,required=false)
    @Out
    protected UserServiceImpl userService;
    
    @In(required = false)
    @Out(required = false)
    private Customer customer;
    @Logger
    protected Log log;

    @In(value = "userBean", create = true, required = false)
    @Out
    protected UserBean userBean;
    
    @In("serviceProvider")
    protected ServiceProvider serviceProvider;
    
    @In(value = "roleService", create = true, required = false)
    @Out
    protected RoleServiceImpl roleService;
    
    public String save(Customer person) {
        customerService.saveOrUpdate(person);
        return "success";
    }

    public String delete(Customer person) {
        customerService.delete(person);
        return "success";
    }

    public String update(Customer person) {
        customerService.saveOrUpdate(person);
        return "success";
    }

    public List<Customer> getAll() {
        return customerService.getAll();
    }

    public Customer getSelectedPerson() {
        return customer;
    }

    public void setSelectedPerson(Customer person) {
        this.customer = person;
    }

    public void refresh() {
        customerService.refresh(customer);
    }

    @Destroy
    @Remove
    public void destroy() {
        log.debug("destry component #0", this);
    }

    public boolean hasUser(Customer person) {
        System.out.println("xxxxxxxxxxxxx");
        System.out.println(person.getUser());
        return person.getUser()!=null;
    }
    
    public String createUser(User user){
        user.setPerson(customer);
        if("".equals(user.getPassword())){
            return "failed";
        }
        
        user.setEnabled(true);
        Role role = roleService.findRoleByName("Klient");
        System.out.println(role);
        user.getRoles().add(role);
        userService.saveOrUpdate(user);
        role.getUsers().add(user);
        roleService.saveOrUpdate(role);
        
        
        IdentityService identityService = serviceProvider.getIdentityService();
        identityService.createUser(user.getUsername(), user.getUsername(), user.getEmail());
        identityService.createMembership(user.getUsername(), "customer-group");
        
//        userBean.
//        String result = userBean.save(user);
//        Role role = roleDao.findRoleByName("Klient");
        //user.getRoles().add(role);
        customer.setUser(user);
        customerService.saveOrUpdate(customer);
        return "success";
    }
    
     @Factory
        public Gender[] getGender() {
                return Gender.values();
        }

 
}
